package com.baihui.crm;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import com.baihui.taobao.db.GeneDAO;
import com.baihui.taobao.util.Util;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Trade;
import com.taobao.api.internal.mapping.ApiField;
import com.taobao.api.internal.mapping.ApiListField;
import com.taobao.api.request.TradeGetRequest;
import com.taobao.api.response.TradeGetResponse;

/**
 * Created by Kevin.
 * User: HSZM1106-0312
 * Date: 12-2-9
 * Time: 下午10:38
 */
public class UserGet {
    public static void main(String[] args) throws Exception{
//        TaobaoClient client=new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "12496172", "b22495d6403b974f76debedb58b94d56");
//        UsersGetRequest req=new UsersGetRequest();
//        req.setFields("user_id,uid,nick,vip_info");
//        req.setNicks("中国张健,jiujiudepuer");
//        try {
//            UsersGetResponse response = client.execute(req);
//            System.out.println(response.getUsers().size());
//        } catch (ApiException e) {
//            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//        }
        
        
        
        TaobaoClient client=new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "12531555", "c0e2ae267999e52e63fde23cfedc140f");
        TradeGetRequest req=new TradeGetRequest();
        req.setFields(parseFields(Trade.class));
        req.setTid(127108675241941l);
        TradeGetResponse response = client.execute(req , "61018248650547b391044b3afab3b6f11222f2c8fa510c796053458");
        System.out.println("responseresponseresponse= " + response.getBody());
        
        
    }
    
    /**
     * 根据
     *
     * @param domainClass
     * @return
     * @throws Exception
     */
    public static String parseFields(Class domainClass) throws Exception {
        StringBuilder fields = new StringBuilder();
        innerParseFields(fields, domainClass, "");
        String ret = fields.toString();
        if (ret.endsWith(",")) {
            ret = ret.substring(0, ret.length() - 1);
        }
        return ret;
    }

    public static void innerParseFields(StringBuilder fields, Class domainClass, String prex) throws Exception {
        try {
            prex = (prex == null) ? "" : prex;

            BeanInfo beanInfo = Introspector.getBeanInfo(domainClass, Object.class);
            PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor pd : descriptors) {
                Field field = domainClass.getDeclaredField(pd.getName());
                if (Util.isExist(field.getName(), GeneDAO.excludefield)) {
                    continue;
                }
                ApiField apiField = field.getAnnotation(ApiField.class);
                if (apiField != null) {
                    if (field.getType().getSimpleName().toUpperCase().equals("LIST")) {
                        ApiListField listField = field.getAnnotation(ApiListField.class);
                        if (listField != null) {
                            Type fieldType = field.getGenericType();
                            if (fieldType instanceof ParameterizedType) {
                                ParameterizedType paramType = (ParameterizedType) fieldType;
                                Type[] genericTypes = paramType.getActualTypeArguments();
                                if (genericTypes != null && genericTypes.length > 0) {
                                    if (genericTypes[0] instanceof Class<?>) {
                                        Class<?> subType = (Class<?>) genericTypes[0];
                                        innerParseFields(fields, subType, listField.value());
                                    }
                                }
                            }
                        }
                    } else {
                        if ("".equals(prex)) {
                            fields.append(apiField.value()).append(",");
                        } else {
                            fields.append(prex + "." + apiField.value()).append(",");
                        }

                    }
                }
            }
        } catch (IntrospectionException e) {
            throw new Exception(e);
        }
    }
}
